/**
 * 本质上就是给N个数，选出N/2对，使得每一对的绝对值之差的和最小
 * 首先排序，N如果是偶数直接选邻近的即可
 * N如果是奇数，则有一个选不进去。枚举这个不选的，然后同样是相邻组对即可
 */
#include <bits/stdc++.h>
using namespace std;

#include <bits/extc++.h>
using namespace __gnu_pbds;

#include <bits/stdc++.h>
using namespace std;

#include <bits/stdc++.h>
using namespace std;

char *__abc147, *__xyz258, __ma369[1000000];
#define __hv007() ((__abc147==__xyz258) && (__xyz258=(__abc147=__ma369)+fread(__ma369,1,100000,stdin),__abc147==__xyz258) ? EOF : *__abc147++)

int getInt(){
	int sgn = 1;
	char ch = __hv007();
	while( ch != '-' && ( ch < '0' || ch > '9' ) ) ch = __hv007();
	if ( '-' == ch ) {sgn = 0;ch=__hv007();}
 
	int ret = (int)(ch-'0');
	while( '0' <= (ch=__hv007()) && ch <= '9' ) ret = ret * 10 + (int)(ch-'0');
	return sgn ? ret : -ret;
}

using llt = long long;
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

int N, K;
vi A;

llt proc(){
    if(K % 2 == 0){
        llt ans = 0;
        for(int i=2;i<=K;i+=2) ans += A[i] - A[i - 1];
        return ans;
    }
    if(1 == K) return 0;

    vector<llt> U(K+2, 0), V(K+2, 0);
    for(int i=2;i<K;i+=2) U[i] = U[i - 2] + A[i] - A[i - 1];
    for(int i=K-1;i>=2;i-=2) V[i] = V[i+2] + A[i + 1] - A[i];

    llt ans = min(U[K - 1], V[2]);
    for(int i=3;i<=K-2;i+=2){
        ans = min(ans, U[i - 1] + V[i + 1]);     
    }
    return ans;
}

int main(){
#ifndef ONLINE_JUDGE
    freopen("z.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(0);
    int nofkase = 1;
    // cin >> nofkase;
    while(nofkase--){
        cin >> N >> K;
        A.assign(K + 1, 0);
        for(int i=1;i<=K;++i) cin >> A[i];
        cout << proc() << "\n";
    }
    return 0;
}